<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>修改角色</title>
    <meta content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no' name='viewport'>
    <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
    <!--[if lt IE 9]>
    <script type="text/javascript"  src="http://www.ijquery.cn/js/html5shiv.js"></script>
    <![endif]-->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script>window.jQuery || document.write('<script src="js/jquery-1.12.4.min.js">\x3C/script>')</script>
    <!--也可以用yepnopejs 加载器;直接是window对象-->
    <!--Font Awesome  一套绝佳的图标字体库和CSS框架 无需依赖JavaScript 无限缩放  完全免费 jquery-confirm里面用到 官网http://fontawesome.dashgame.com/-->
    <link href="css/font-awesome/css/font-awesome.min.css" rel="stylesheet">
    <!-- bootstrap 3.0.2 -->
    <link href="css/bootstrap-3.2.0/css/bootstrap.css" rel="stylesheet" type="text/css"/>
    <link href="css/bootstrap-box.css" rel="stylesheet" type="text/css"/>
    <link href="css/bootstrap-page.css" rel="stylesheet" type="text/css"/>
    <link href="js/jstree/themes/default/style.min.css" rel="stylesheet" type="text/css"/>
    <link href="js/nice-validator/jquery.validator.css" rel="stylesheet" type="text/css"/>
    <link href="js/jquery-confirm/jquery-confirm.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="js/bootstrap.min.js"></script>
    <script type="text/javascript" src="js/jquery-confirm/jquery-confirm.js"></script>
    <script type="text/javascript" src="js/jsviews.js"></script>
    <script type="text/javascript" src="js/loadTmpl.js"></script>
    <script src="js/base.js" type="text/javascript"></script>
    <script type="text/javascript" src="js/jstree/jstree.min.js"></script>
    <script type="text/javascript" src="js/nice-validator/jquery.validator.min.js"></script>
    <script type="text/javascript" src="js/nice-validator/local/zh-CN.js"></script>
    <script type="text/javascript" src="js/layer/layer.js"></script>
    <script type="text/javascript">
        $(function () {
            var roleId = getUrlParam("roleId");
            if (roleId == "") {
                myAlert("roleId参数不能为空");
                return;
            }
            $("#roleId").val(roleId);
            $.ajax({
                url: root + 'xaCmsRole/showRole/' + roleId,
                type: 'get',
                dataType: 'json',
                success: function (data) {
                    $("#roleName").val(data.roleName);
                    $("#roleDesc").val(data.roleDesc);
                }
            });
            $("#resource").jstree({
                "core": {
                    "themes": {
                        "variant": "large"
                    },
                    'data': {
                        'url': root + 'xaCmsRole/showResource/' + roleId
                    }
                },
                "plugins": ["wholerow", "checkbox"]
            });

            $("#validateForm").validator({
                    valid: function (form) {
                        var selectArray = $('#resource').jstree('get_selected');		//选中项
                        var roleName = $("#roleName").val();
                        var roleId = $("#roleId").val();
                        var roleDesc = $("#roleDesc").val();
                        if (selectArray.length == 0) {
                            myAlert("请选择角色对应的资源");
                            return;
                        }
                        var xaCmsRole = {"roleId": roleId, "roleName": roleName, "roleDesc": roleDesc};
                        var resourceIds = "" + selectArray + "";
                        $.ajax({
                            url: root + 'xaCmsRole/' + resourceIds + "/update",
                            type: 'POST',
                            datType: 'json',
                            contentType: "application/json",
                            data: JSON.stringify(xaCmsRole),
                            success: function (data) {
                                if (data.status = 1) {
                                    window.location.href = "role.html";
                                }
                            }
                        });
                    }
                }).on("click", "#updateSome", function (e) {
                    $(e.delegateTarget).trigger("validate");
                });

        });
    </script>
</head>
<body>
<section>
    <div class="col-xs-12">
        <form id="validateForm" class="form-horizontal">
            <div class="box">
                <div class="span1">
                    <span class="badge badge-info">后台管理    &gt;  角色列表    &gt;  角色编辑</span>
                </div>
                <div class="box-header">
                    <input type="button" value="确定" id="updateSome" class="btn btn-success">
                    <a href="role.html" class="btn btn-info">返回</a>
                </div>
            </div>
            <input type="hidden" id="roleId" name="roleId"/>
            <div class="box-body table-responsive">
                <div class="form-group">
                    <label for="" class="col-sm-2 control-label">角色：</label>
                    <div class="col-sm-9">
                        <input type="text" id="roleName" name="roleName" class="form-control"
                               data-rule="角色:required;length[2~30];remote[post:xaCmsRole/checkRole, roleId]"
                               placeholder="请输入角色名"/>
                    </div>
                </div>
                <div class="form-group">
                    <label for="" class="col-sm-2 control-label">描述：</label>
                    <div class="col-sm-9">
                        <textarea id="roleDesc" class="form-control" rows="3"
                                  data-rule="描述:required;length[1~200, true]" cols="50"></textarea>
                    </div>
                </div>
                <div id="resource"></div>
            </div>
        </form>
    </div>
</section>
</body>
</html>